/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import com.google.common.collect.HashMultimap;
/*     */ import com.google.common.collect.Multimap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.util.ZimbraApplication;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Date;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DbPendingAclPush
/*     */ {
/*  34 */   public static String TABLE_PENDING_ACL_PUSH = "pending_acl_push";
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*  39 */   private static boolean supported = ZimbraApplication.getInstance().supports(DbPendingAclPush.class);
/*     */   
/*     */   public static void queue(Mailbox mbox, int itemId) throws ServiceException
/*     */   {
/*  43 */     if (!supported)
/*  44 */       return;
/*  45 */     if (mbox == null)
/*  46 */       return;
/*  47 */     ZimbraLog.mailbox.debug("Queuing for ACL push - mailbox %s item %s", new Object[] { Integer.valueOf(mbox.getId()), Integer.valueOf(itemId) });
/*  48 */     DbPool.DbConnection conn = mbox.getOperationConnection();
/*  49 */     PreparedStatement stmt = null;
/*  50 */     boolean supportsReplace = Db.supports(Db.Capability.REPLACE_INTO);
/*     */     try
/*     */     {
/*  53 */       stmt = conn.prepareStatement("SELECT mailbox_id, item_id, date FROM " + TABLE_PENDING_ACL_PUSH + " WHERE mailbox_id = ? AND item_id = ? ");
/*     */       
/*  55 */       stmt.setInt(1, mbox.getId());
/*  56 */       stmt.setInt(2, itemId);
/*  57 */       ResultSet rs = stmt.executeQuery();
/*     */       
/*  59 */       if (rs.next()) {
/*  60 */         stmt = conn.prepareStatement("UPDATE " + TABLE_PENDING_ACL_PUSH + " SET date = ? WHERE mailbox_id = ? AND item_id = ?");
/*     */         
/*     */ 
/*  63 */         stmt.setLong(1, System.currentTimeMillis());
/*  64 */         stmt.setInt(2, mbox.getId());
/*  65 */         stmt.setInt(3, itemId);
/*  66 */         stmt.executeUpdate();
/*     */       }
/*     */       else {
/*  69 */         String command = supportsReplace ? "REPLACE" : "INSERT";
/*  70 */         stmt = conn.prepareStatement(command + " INTO " + TABLE_PENDING_ACL_PUSH + " (mailbox_id, item_id, date) VALUES (?, ?, ?)");
/*     */         
/*  72 */         stmt.setInt(1, mbox.getId());
/*  73 */         stmt.setInt(2, itemId);
/*  74 */         stmt.setLong(3, System.currentTimeMillis());
/*  75 */         stmt.executeUpdate();
/*     */       }
/*     */     }
/*     */     catch (SQLException e) {
/*  79 */       throw ServiceException.FAILURE("Unable to queue for ACL push - mailbox " + mbox.getId() + " item " + itemId, e);
/*     */     }
/*     */     finally {
/*  82 */       conn.closeQuietly(stmt);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  89 */     if (!supportsReplace) {
/*     */       try {
/*  91 */         Thread.sleep(2L);
/*     */       }
/*     */       catch (InterruptedException ignored) {}
/*     */     }
/*     */   }
/*     */   
/*     */   public static Multimap<Integer, Integer> getEntries(Date uptoTime) throws ServiceException {
/*  98 */     Multimap<Integer, Integer> mboxIdToItemIds = HashMultimap.create();
/*  99 */     DbPool.DbConnection conn = null;
/* 100 */     PreparedStatement stmt = null;
/* 101 */     ResultSet rs = null;
/* 102 */     ZimbraLog.misc.debug("Getting entries recorded before %s for ACL push", new Object[] { uptoTime });
/*     */     try {
/* 104 */       conn = DbPool.getConnection();
/* 105 */       stmt = conn.prepareStatement("SELECT mailbox_id, item_id FROM " + TABLE_PENDING_ACL_PUSH + " WHERE date < ?");
/*     */       
/* 107 */       stmt.setLong(1, uptoTime.getTime());
/* 108 */       rs = stmt.executeQuery();
/* 109 */       while (rs.next()) {
/* 110 */         mboxIdToItemIds.put(Integer.valueOf(rs.getInt(1)), Integer.valueOf(rs.getInt(2)));
/*     */       }
/*     */     } catch (SQLException e) {
/* 113 */       throw ServiceException.FAILURE("Unable to get entries recorded before " + uptoTime + " for ACL push", e);
/*     */     } finally {
/* 115 */       DbPool.closeResults(rs);
/* 116 */       DbPool.closeStatement(stmt);
/* 117 */       DbPool.quietClose(conn);
/*     */     }
/* 119 */     return mboxIdToItemIds;
/*     */   }
/*     */   
/*     */   public static void deleteEntries(Date uptoTime) throws ServiceException {
/* 123 */     ZimbraLog.misc.debug("Deleting entries for ACL push before %s", new Object[] { uptoTime });
/* 124 */     DbPool.DbConnection conn = null;
/* 125 */     PreparedStatement stmt = null;
/*     */     try {
/* 127 */       conn = DbPool.getConnection();
/* 128 */       stmt = conn.prepareStatement("DELETE FROM " + TABLE_PENDING_ACL_PUSH + " WHERE date < ?");
/*     */       
/* 130 */       stmt.setLong(1, uptoTime.getTime());
/* 131 */       stmt.executeUpdate();
/* 132 */       conn.commit();
/*     */     } catch (SQLException e) {
/* 134 */       throw ServiceException.FAILURE("Unable to delete UID's", e);
/*     */     } finally {
/* 136 */       DbPool.closeStatement(stmt);
/* 137 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteEntry(int mailboxId, int itemId) throws ServiceException {
/* 142 */     ZimbraLog.misc.debug("Deleting entry for ACL push for mailbox: %d, item : %d", new Object[] { Integer.valueOf(mailboxId), Integer.valueOf(itemId) });
/* 143 */     DbPool.DbConnection conn = null;
/* 144 */     PreparedStatement stmt = null;
/*     */     try {
/* 146 */       conn = DbPool.getConnection();
/* 147 */       stmt = conn.prepareStatement("DELETE FROM " + TABLE_PENDING_ACL_PUSH + " WHERE mailbox_id=? AND" + " item_id=?");
/*     */       
/*     */ 
/* 150 */       stmt.setInt(1, mailboxId);
/* 151 */       stmt.setInt(2, itemId);
/* 152 */       stmt.executeUpdate();
/* 153 */       conn.commit();
/*     */     } catch (SQLException e) {
/* 155 */       throw ServiceException.FAILURE("Unable to delete UID's", e);
/*     */     } finally {
/* 157 */       DbPool.closeStatement(stmt);
/* 158 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/DbPendingAclPush.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */